package com.xiangban.yanglaodajie_platform.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiangban.yanglaodajie_platform.dao.TakesMapper;
import com.xiangban.yanglaodajie_platform.dao.UserMapper;
import com.xiangban.yanglaodajie_platform.entity.Takes;
import com.xiangban.yanglaodajie_platform.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/4/16 14:58
 */
@Service
public class TakesService {

    @Autowired
    private TakesMapper takesMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private CommonUtils commonUtils;

    public Takes getUserInfo(Takes takes){
        takes.setUserId(Integer.parseInt(commonUtils.getUserId(request)));
        LambdaQueryWrapper<Takes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Takes::getUserId,takes.getUserId());
        Takes takes1 = takesMapper.selectOne(queryWrapper);
        if (takes1 != null){
            return takes1;
        }
        Takes userInfoById = takesMapper.getUserInfoById(takes);
        return userInfoById;
    }

    public void updateUserInfo(Takes takes) throws ParseException {
        takes.setUserId(Integer.parseInt(commonUtils.getUserId(request)));
        String takesTime = takes.getTakesTime();
        takes.setTakesTime(takes.getTakesTime());

        LambdaQueryWrapper<Takes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Takes::getUserId,takes.getUserId());
        Takes takes1 = takesMapper.selectOne(queryWrapper);
        if (takes1 == null){
            takesMapper.insert(takes);
        }else {
            takesMapper.updateUserInfoByUserId(takes);
        }
    }
}
